<?php
include("./config.php");
include("./utils.php");

$op = $_POST['op'];
$appid=$_POST['appid'];
$id=$_POST['id'];
$value = "";
if (isset($_POST['value'])){
  $value = $_POST['value'];
}

function get_sql($op, $appid, $id, $value){
  $sql = "";
  $appid = safe($appid);
  $id = safe($id);
  $value = safe($value);
  switch($op){

  # -------------------------------------------------------
  # 保存信息
  case 'save-note':
    $sql = "UPDATE `familya_item` SET `note`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-name':
    $sql = "UPDATE `familya_item` SET `name`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-parentid':
    $sql = "UPDATE `familya_item` SET `parentid`=$value WHERE `id`=$id LIMIT 1";
    break;
  case 'save-generation':
    $sql = "UPDATE `familya_item` SET `generation`=$value WHERE `id`=$id LIMIT 1";
    break;
  case 'save-addr':
    $sql = "UPDATE `familya_item` SET `addr`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-sex':
    $sql = "UPDATE `familya_item` SET `sex`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-born-time':
    $sql = "UPDATE `familya_item` SET `born_time`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-sex':
    $sql = "UPDATE `familya_item` SET `sex`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-die-time':
    $sql = "UPDATE `familya_item` SET `die_time`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-wife-name':
    $sql = "UPDATE `familya_item` SET `wife_name`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-wife-born-time':
    $sql = "UPDATE `familya_item` SET `wife_born_time`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-wife-die-time':
    $sql = "UPDATE `familya_item` SET `wife_die_time`='$value' WHERE `id`=$id LIMIT 1";
    break;
  case 'save-wife-addr':
    $sql = "UPDATE `familya_item` SET `wife_addr`='$value' WHERE `id`=$id LIMIT 1";
    break;
    
  # -------------------------------------------------------
  case 'add-app':
    # 插入软件
    $userid = safe($_POST['userid']);
    $logo = safe($_POST['logo']);
    $des = safe($_POST['des']);
    $name = safe($_POST['name']);
    $skin = safe($_POST['skin']);
    $name_en = safe($_POST['name_en']);
    $des_en = safe($_POST['des_en']);
    $sql = "INSERT INTO `app`(`name`, `des`, `name_en`, `des_en`, `skin`, `logo`) VALUES('$name', '$des', '$name_en', '$des_en', '$skin', '$logo')";
    break;
  case 'add-item':
    # 插入软件
    $userid = safe($_POST['userid']);
    $appid = safe($_POST['appid']);
    $name = safe($_POST['name']);
    $des = safe($_POST['des']);
    $item = safe($_POST['item']);
    $skin = safe($_POST['skin']);
    $name_en = safe($_POST['name_en']);
    $des_en = safe($_POST['des_en']);
    $context = safe($_POST['context']);
    $context_en = safe($_POST['context_en']);
    $sql = "INSERT INTO `familya_item`(`appid`, `familya_item`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`) VALUES('$appid', '$item', '$name', '$des', '$context', '$name_en', '$des_en', '$context_en', '$skin')";
    break;
  case 'add-items':
    # 插入软件
    $userid = safe($_POST['userid']);
    $data = $_POST['data'];
    $appid = safe($_POST['appid']);
    if (!$appid){
      echo '{code:10002, msg:"no appid"}';
      exit(0);
    }

    $sql = 'INSERT INTO `familya_item`(`appid`, `familya_item`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`) VALUES ';
    foreach($data as $k => $item) {
        $name = trim(safe($item['name']));
        $des = trim(safe($item['des']));
        $item = trim(safe($item['item']));
        if($item=="" || $name==""){
          continue;
        }
        $skin = safe($item['skin']);
        $name_en = safe($item['name_en']);
        $des_en = safe($item['des_en']);
        $context = safe($item['context']);
        $context_en = safe($item['context_en']);
        $sql .= "('$appid', '$item', '$name', '$des', '$context', '$name_en', '$des_en', '$context_en', '$skin'),";
    }
    $sql = rtrim($sql, ',');

    //// TODO
    //var_dump($_POST);
    //var_dump($sql);
    //$sql="";

    break;

  case 'add-sns':
    # 插入评论
    $userid = safe($_POST['userid']);
    $appid = safe($_POST['appid']);
    $id = safe($_POST['id']);
    $content = safe($_POST['content']);
    $status = safe($_POST['status']);
    $parent = safe($_POST['parent']);
    $sql = "INSERT INTO `sns`(`userid`, `appid`, `id`, `parent`, `content`, `status`) VALUES('$userid', '$appid', '$id', '$parent', '$content', '$status')";
    break;


  # -------------------------------------------------------
  # 评论相关
  case 'set-sns-status':
    # 添加评论的状态
    $id = safe($_POST['id']);
    $sql = "UPDATE `sns` SET `status=`status`|$status WHERE `id`='$id' LIMIT 1";
    break;
  # -------------------------------------------------------


  case 'set-info':
    $sql = "INSERT INTO `info`(`appid`, `id`, `value`) VALUES($appid, $id, $value) ON DUPLICATE KEY UPDATE `value`='$value'";
    break;
  case 'change-info':
    $sql = "INSERT INTO `info`(`appid`, `id`, `value`) VALUES($appid, $id, $value) ON DUPLICATE KEY UPDATE `value`=`value`+'$value'";
    break;
  case 'clear-info':
    $sql = "DELETE FROM `info` WHERE `id`=$id AND `appid`=$appid";
    break;
  case 'set-str':
    $sql = "INSERT INTO `str`(`appid`, `id`, `value`) VALUES($appid, $id, '$value') ON DUPLICATE KEY UPDATE `value`='$value'";
    break;


  default:
    break;
  }
  return $sql;

}


$sql = get_sql($op, $appid, $id, $value);
#echo $sql;
$state = 0;
if($sql != ""){
    $state = mysql_query($sql);
    if (state && strpos($op, 'add')!=FALSE){
      $state = mysql_insert_id();
    }
}

echo $state;

?>

